<template>
  <div class="m-rich-editor">
    <div id="editorBar" class="rich-editor-bar">
      <div class="dropdown-bd">
        <p class="dropdown-tit">H</p>
        <div class="dropdown-list">
          <button class="size1" size="6" title="1级" ec="FontSize" unselectable="on">1</button>
          <button class="size2" size="5" title="2级" ec="FontSize" unselectable="on">2</button>
          <button class="size3" size="4" title="3级" ec="FontSize" unselectable="on">3</button>
        </div>
      </div>
      <button title="加粗" ec="bold" unselectable="on">粗</button>
      <button title="斜体" ec="italic" unselectable="on">斜</button>
      <button title="下划线" ec="underline" unselectable="on">划</button>
      <button title="删除线" ec="StrikeThrough" unselectable="on">删</button>
      <span>|</span>
      <button title="左对齐" ec="justifyleft" unselectable="on">左</button>
      <button title="居中对齐" ec="justifycenter" unselectable="on">中</button>
      <button title="右对齐" ec="justifyright" unselectable="on">右</button>
      <span>|</span>
      <button title="有序列表" ec="insertorderedlist" unselectable="on">有序</button>
      <button title="无序列表" ec="insertunorderedlist" unselectable="on">无序</button>
      <!-- <button title="引用" ec="blockquote" unselectable="on">引用</button> -->
      <span>|</span>
      <div class="dropdown-bd">
        <p class="dropdown-tit">图片</p>
        <div class="dropdown-list">
          <button title="本地图片" flag="local" ec="insertImage" unselectable="on">本地</button>
          <button title="远程图片" flag="remote" ec="insertImage" unselectable="on">远程</button>
        </div>
      </div>
      <button title="图片链接" ec="createLink" unselectable="on">链接</button>
    </div>
    <textarea id="richEditorCon" spellcheck="false" :value="value"></textarea>
  </div>
</template>

<script>
  import RichEditor from './editor'
  import { imgUploadUrl } from 'js/config'
  import { mapGetters } from 'vuex'

  export default {
    props: {
      value: [String]
    },
    data() {
      return {
        iframeDoc: null // iframe文档
      }
    },
    computed: {
      ...mapGetters(['token', 'goodsId', 'storeId'])
    },
    mounted() {
      console.log(this.goodsId)
    },
    methods: {
      _initRichEditor() {
        const goodsId = this.$route.params.gdId ? this.$route.params.gdId : this.goodsId
        new RichEditor('editorBar', 'richEditorCon', this.token, goodsId, this.storeId, val => {
          this.$emit('input', val)
        })
      }
    },
    watch: {
      value(newVal, oldVal) {
        if (newVal != oldVal) {
          document.getElementsByName('editor')[0].contentDocument.getElementsByTagName('body')[0].innerHTML = newVal
        }
      },
      goodsId(newVal, oldVal) {
        if (newVal) {
          this._initRichEditor()
        }
      }
    }
  }
</script>

<style lang="scss" scoped>
  // 编辑框
  .m-rich-editor {
    position: relative;
    width: 100%;
    margin: 0 auto;
    border: 1px solid #dddddd;
    border-top-color: #ebebeb;
    border-radius: 4px;
    background: #fff;
    box-shadow: 0 1px 1px #d3d1d1;
  }

  // 编辑操作条
  .rich-editor-bar {
    border-bottom: 1px solid #eee;
    font-size: 0;

    .dropdown-bd {
      position: relative;
      display: inline-block;

      &:hover {
        .dropdown-list {
          display: block;
        }
      }
    }

    .dropdown-list {
      position: absolute;
      display: none;

      label {
        input {
          opacity: 0;
          width: 0;
          height: 0;
        }
      }

      button {
        display: block;
        height: 30px;
        line-height: 30px;
        background: #fff;
      }

      .size1 {
        font-size: 18px;
      }
      .size2 {
        font-size: 16px;
      }
      .size3 {
        font-size: 14px;
      }
    }

    button, .dropdown-tit {
      display: inline-block;
      height: 40px;
      line-height: 40px;
      padding: 0 12px;
      text-align: center;
      font-size: 14px;
      color: rgb(20, 68, 101);
      font-weight: 700;
      user-select: none;
      cursor: pointer;
    }

    span {
      display: inline-block;
      height: 25px;
      line-height: 25px;
      padding: 0 0px;
      margin-right: 4px;
      color: #e5e5e5;
      font-size: 12px;
      cursor: default;
    }
  }

  // 编辑内容
  #richEditorCon {
    width: 1200px;
    height: 800px;
    overflow: auto;
    border: 0;
    font-size: 14px;
    background: transparent;
  }
</style>